package com.example.springdemo.thread;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolPractice {


    public static void main(String[] args) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10,10,0L, TimeUnit.DAYS,new LinkedBlockingDeque<>());
        for (int i = 0;i<15;i++){
            int finalI = i;
            threadPoolExecutor.execute(()->{
                System.out.println("i:"+ finalI);
                System.out.println("1111threadNmae:"+ Thread.currentThread().getName());
                int time = 500;
                int totalTime = 0;
                while (true){
                    try {
                        Thread.sleep(time);
                        totalTime = totalTime + time;
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if (totalTime >= 3000){
                        break;
                    }

                }
                /**
                 * 开始就申请完了10个线程，这10个线程里面又想申请子线程，但是申请的时候10个已经用完，并且有等待阻塞的条件，导致线程死锁。
                 */
                CountDownLatch countDownLatch = new CountDownLatch(1);
                threadPoolExecutor.execute(()->{
                    System.out.println("2222threadNmae:"+ Thread.currentThread().getName());
                    countDownLatch.countDown();
                });
                try {
                    countDownLatch.await();
                    System.out.println(" countDownLatch.await()");
                }catch (Exception e){
                }

            });
        }
    }


}
